package com.example.stars_project_android.index;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.example.stars_project_android.R;
import com.example.stars_project_android.adapter.GuestBookAdapter;
import com.example.stars_project_android.entity.GuestBookEntity;
import com.example.stars_project_android.util.Constant;
import com.sackcentury.shinebuttonlib.ShineButton;

import java.util.ArrayList;
import java.util.List;

/**
 * 留言板界面
 */
public class Guestbook extends AppCompatActivity implements View.OnClickListener{

    private ShineButton likeBtn;
    private ListView listview;
    private List<GuestBookEntity> myList = new ArrayList<>();
    private TextView tvRemain,tvLikeNumber;   //提示文本
    private ImageButton GuestBookToRoomDetail, share;//返回到首页
    private Button MessageDelete;//删除小提示按钮
    private Button PostGuestBook;//发布留言按钮
    private boolean shineBtn = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guestbook);

        setView();
        //设置数据源
//        myList = Constant.guestBookList;
        myList = setData();
        //初始化适配器
        GuestBookAdapter guestBookAdapter = new GuestBookAdapter(
                this,
                R.layout.guestbook_item,
                myList
        );

        //绑定适配器
        listview.setAdapter(guestBookAdapter);
        View view = View.inflate(this,R.layout.guestbook_item,null);
        likeBtn = view.findViewById(R.id.btn_heart);
        likeBtn.init(this);
        likeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (shineBtn = false){
                    String number = tvLikeNumber.getText().toString();
                    int num = Integer.parseInt(number) + 1;
                    tvLikeNumber.setText(String.valueOf(num));
                    shineBtn = true;
                }else{
                    String number = tvLikeNumber.getText().toString();
                    int num = Integer.parseInt(number) - 1;
                    tvLikeNumber.setText(String.valueOf(num));
                    shineBtn = false;
                }
            }
        });
    }

    private List<GuestBookEntity> setData() {
        if (Constant.guestBookList.size() == 0) {
            List<GuestBookEntity> myList = new ArrayList<>();
            GuestBookEntity guestBook = new GuestBookEntity();
            guestBook.setUserId("TYC 4726-134-1");
            guestBook.setUserMessage("今天的天气很不错哦，适合出游！");
            guestBook.setMessageTime("2022-11-04 13:19:43");
            guestBook.setCommentsNum("45");
            guestBook.setLikeNum("34");
            myList.add(guestBook);
            GuestBookEntity guestBook1 = new GuestBookEntity();
            guestBook1.setUserId("TYC 4726-134-1");
            guestBook1.setUserMessage("卿卿日常真的太好看啦！");
            guestBook1.setMessageTime("2022-11-14 12:04:32");
            guestBook1.setCommentsNum("199");
            guestBook1.setLikeNum("308");
            myList.add(guestBook1);
            GuestBookEntity guestBook2 = new GuestBookEntity();
            guestBook2.setUserId("TYC 4726-134-1");
            guestBook2.setUserMessage("天冷了，注意保暖哦");
            guestBook2.setMessageTime("2022-12-02 08:27:13");
            guestBook2.setCommentsNum("35");
            guestBook2.setLikeNum("13");
            myList.add(guestBook2);
            GuestBookEntity guestBook3 = new GuestBookEntity();
            guestBook3.setUserId("TYC 4726-134-1");
            guestBook3.setUserMessage("天冷了，注意保暖哦");
            guestBook3.setMessageTime("2022-12-02 08:27:13");
            guestBook3.setCommentsNum("35");
            guestBook3.setLikeNum("13");
            myList.add(guestBook3);
            GuestBookEntity guestBook4 = new GuestBookEntity();
            guestBook4.setUserId("TYC 4726-134-1");
            guestBook4.setUserMessage("天冷了，注意保暖哦");
            guestBook4.setMessageTime("2022-12-02 08:27:13");
            guestBook4.setCommentsNum("35");
            guestBook4.setLikeNum("13");
            myList.add(guestBook4);
            Constant.guestBookList = myList;
            return myList;
        } else {
            myList = Constant.guestBookList;
            return myList;
        }
    }

    private void setView() {
//        listview = findViewById(R.id.lv_guestbook);
        tvRemain = findViewById(R.id.tv_remind);
        share = findViewById(R.id.ib_message_share);
        share.setOnClickListener(this);
//        GuestBookToRoomDetail = findViewById(R.id.ib_message_return);
        GuestBookToRoomDetail.setOnClickListener(this);
        MessageDelete = findViewById(R.id.btn_message_delete);
        MessageDelete.setOnClickListener(this);
        PostGuestBook = findViewById(R.id.btn_post_guestbook);
        PostGuestBook.setOnClickListener(this);
        tvLikeNumber = findViewById(R.id.tv_like_number);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
//            case R.id.ib_message_return:
//                //点击返回按钮
//                Intent intent1 = new Intent();
//                intent1.setClass(Guestbook.this, Index.class);//返回房间详情页面
//                startActivity(intent1);
//                break;
            case R.id.ib_message_share:
                //点击分享按钮

                break;
            case R.id.btn_message_delete:
                //点击删除小提示的按钮
                tvRemain.setText("");
                break;
            case R.id.btn_post_guestbook:
                //点击发布留言
                Intent intent2 = new Intent();
                intent2.setClass(Guestbook.this, Postmessage.class);//跳转到发布留言页面
                startActivity(intent2);
                break;
        }

    }
}